package com.x.component.bluetooth;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.tech.bt_libray.BTSettingClient;
import com.tech.bt_libray.TechBTMusicServer;
import com.tech.bt_libray.TechBTSettingServer;
import com.techbt.contants.CarsysoBTClientInitParams;
import com.techbt.contants.CarsysoBTSettingStateConstants;
import com.techbt.core.TechBTClient;
import com.techbt.core.TechBTClientConfiguration;
import com.techbt.iface.TechServiceBindListener;
import com.techbt.pojo.DeviceItem;
import com.x.component.widget.MediaWidgetInfo;
import com.x.component.widget.MediaWidgetTool;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadata;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

public class BluetoothComponent {

	private static BluetoothComponent mInstance = null;

	private static final Object mLock = new Object();

	private Context mContext;

	private TechBTSettingServer mBTSettingServer;

	private static final String TAG = "BluetoothComponent";

	private int HfpState;

	private Handler foregroundHandler;

	private BluetoothComponent() {
		foregroundHandler = new Handler(Looper.getMainLooper()) {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 101:
					for(BluetoothTool tool:mBluetoothToolList) {
						tool.onHfpStateChanged(msg.arg1);
					}
					break;
				case 100:
					for(BluetoothTool tool:mBluetoothToolList) {
						tool.onHfpStateChanged(msg.arg1);
					}
					break;

				default:
					break;
				}
			}
		};
	}

	public static BluetoothComponent getInstance() {
		if (null == mInstance) {
			synchronized (mLock) {
				if (null == mInstance) {
					mInstance = new BluetoothComponent();
				}
			}
		}
		return mInstance;
	}

	public void init(Context context) {
		this.mContext = context;
		if (this.mContext != null) {
			initBTClientConfig();
		}
	}

	private CopyOnWriteArrayList<BluetoothTool> mBluetoothToolList = new CopyOnWriteArrayList<BluetoothTool>();

	public boolean setBluetoothTool(BluetoothTool BluetoothTool) {
		if (BluetoothTool != null && !mBluetoothToolList.contains(BluetoothTool)) {
			mBluetoothToolList.add(BluetoothTool);
			return true;
		}

		return false;
	}

	public boolean removeBluetoothTool(BluetoothTool BluetoothTool) {
		if (BluetoothTool != null && mBluetoothToolList.contains(BluetoothTool)) {
			mBluetoothToolList.remove(BluetoothTool);
			return true;
		}
		return false;
	}

	// init and connect to BT Server
	public void initBTClientConfig() {
		if (!TechBTClient.getInstance().isAidlBind()) {
			Log.d(TAG, "initBTClientConfig: init BT Client");
			CarsysoBTClientInitParams params = new CarsysoBTClientInitParams();
			TechBTClientConfiguration.getInstance().initBTSDKClient(params);
			TechBTClient.getInstance().registServiceBindListener(new TechServiceBindListener() {

				@Override
				public void onServiceUnBind() {
					Log.d(TAG, "initBTClientConfig: onServiceUnBind");
					if (mBTSettingServer != null) {
						try {
							mBTSettingServer.unregisterCallback(settingCallback);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				@Override
				public void onServiceBind() {
					Log.d(TAG, "initBTClientConfig: onServiceBind");
					mBTSettingServer = TechBTClient.getInstance().getSettingServer();
					mBTMusicServer = TechBTClient.getInstance().getMusicServer();
					try {
						settingCallback = new BTSettingCallback();
						mBTSettingServer.registerCallback(settingCallback);
						setHfpState(mBTSettingServer.getHfpConnectState());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			TechBTClient.getInstance().doServiceConnect(mContext.getApplicationContext());
		}
	}

	public int getHfpState() {
		return HfpState;
	}

	private void setHfpState(int hfpState) {
		HfpState = hfpState;
	}

	private class BTSettingCallback extends BTSettingClient.Stub {

		@Override
		public void onBTEnabledStateChanged(int enabled) throws RemoteException {
			Message msg = foregroundHandler.obtainMessage(101);
			msg.arg1 = enabled;
			msg.sendToTarget();
		}

		@Override
		public void onBatteryLevelChange(int level) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onBleStateChanged(int state) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onHfpStateChanged(int state) throws RemoteException {
			Log.d(TAG, "onHfpStateChanged: " + state);
			setHfpState(state);
			if (state == CarsysoBTSettingStateConstants.HFP_CONNECTED) {
				Log.d(TAG, "onHfpStateChanged: " + state + " HFP_CONNECTED");
			} else if (state == CarsysoBTSettingStateConstants.HFP_DISCONNECTED) {
				Log.d(TAG, "onHfpStateChanged: " + state + " HFP_DISCONNECTED");
			}
			Message msg = foregroundHandler.obtainMessage(100);
			msg.arg1 = state;
			msg.sendToTarget();

		}

		@Override
		public void onModifyLocalBTNameSuccess(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onModifyLocalBTPasswordSuccess(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onModifyPairedResult(boolean arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPairedDeviceListLoadFinished(List<DeviceItem> arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPairedDeviceListLoading(DeviceItem arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPairedDeviceModelChange(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onRemotePairedDeviceMacAddressChanged(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onRemotePairedDeviceNameChanged(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSearchBTDevicesFinished(List<DeviceItem> arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSearchingBTDevice(DeviceItem arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSignalStrengthChange(int arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTeleComServiceNameChange(String arg0) throws RemoteException {
			// TODO Auto-generated method stub

		}

	}

	private BTSettingCallback settingCallback;

	private TechBTMusicServer mBTMusicServer;

}
